<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class Users_model extends CF_Model{
	/*
	 * bool create
	 * Creates a user.
	 * @param string username
	 * @param string display
	 * @param string password PLAINTEXT
	 * @param string email
	 * @param int group
	 */
	function create($username, $display, $password, $email, $group) {
		global $DB, $Encryption;
		$data = array(
			"username" => $username,
			"display" => $display,
			"email" => $email,
			"password" => $Encryption->Encrypt($password),
			"group" => $group
		);
		$q = $DB->Driver->insert_query("users", $data);
		return $q;
	}
	
	//--------------------------------------------------------------
	
	/*
	 * array retrieve
	 * Retrieves user using a determined mode
	 * @param string mode="all"
	 * @param mixed data=""
	 * mode all: Selects all users
	 * mode singleid: Pass UID to data: Selects user with UID
	 * mode singleuname: Pass Username to data: Selects user with uname
	 * mode last: Pass Number of last users to get to data: Selects latest DATA registered users
	 */
	function retrieve($mode = "all", $data = "") {
		global $DB;
		// Little Quirks to avoid mistyping
		// Wait... why am I doing this?
		/*
		if(!IN_STRICT) {
			if($mode == "singleuid") $mode = "singleid";
			if($mode == "singlename") $mode = "singleuname";
		} else {
			
		}
		*/
		switch($mode) {
			case 'all':
				$query = $DB->Driver->simple_select("users");
				if($DB->Driver->num_rows($query) == 0) return false;
				else {
					while($array = $DB->Driver->fetch_array($query)) {
						$data[] = $array;
					}
					return $data;
				}
			break;
			case 'singleid':
				$query = $DB->Driver->simple_select("users", "*", "`id` = {$data}");
				if($DB->Driver->num_rows($query) == 0) return false;
				else return $DB->Driver->fetch_array($query);
			break;
			case 'singleuname':
				$query = $DB->Driver->simple_select("users", "*", "`username` = '{$data}'");
				if($DB->Driver->num_rows($query) == 0) return false;
				else return $DB->Driver->fetch_array($query);
			break;
			case 'last':
				$query = $DB->Driver->simple_select("users", "*", "", array(
					array("order", "joined", "DESC"),
					array("limit", $data)
				));
				if(intval($data) == 1){
					if($DB->Driver->num_rows($query) == 0) return false;
					else
						return $DB->Driver->fetch_array($query);
				}
				else {
					if($DB->Driver->num_rows($query) == 0) return false;
					else{
							while($array = $DB->Driver->fetch_array($query)) {
								$data[] = $array;
							}
							return $data;
					}
				}
			break;	
		}
	}
	
	//--------------------------------------------------------------
	
	/*
	 * bool update
	 * Updates a determined user using a determined mode
	 * @param array identifier
	 * @param array data
	 * identifier array = [0] => string id/username, [1] => mixed value (id/username)
	 * data array [key] => value, [key] => value...
	 */
	function update($identifier, $data) {
		global $DB;
		return $DB->Driver->update_query("users", $identifier, $data);
	}
	
	//--------------------------------------------------------------
	
	/*
	 * bool delete
	 * Deletes a determined user using a determined mode
	 * @param array identifier
	 * identifier array = [0] => string id/username, [1] => mixed value (id/username)
	 */
	function delete($identifier) {
		global $DB;
		return $DB->Driver->delete_query("users", $identifier);
	}

}
